
/**
 * /home/admin/prjs/bak/oled_page/build/bin/oled_page
 * 
 * /boot/firmware/config.txt  配置CPU、GPU超频和i2c频率
 * 
 * sudo systemctl daemon-reexec
 * sudo systemctl daemon-reload
 * sudo systemctl enable oled-page.service
 * sudo systemctl start oled-page.service
 * sudo systemctl status oled-page.service
 * 
 * sudo systemctl daemon-reload
 * sudo systemctl restart oled-page.service
 * 
 * sudo systemctl stop oled-page.service
 * sudo systemctl disable oled-page.service
 * 
 * sudo journalctl -u oled-page.service  //查看日志
 * 
 * stress-ng --cpu 0   // CPU压测指令
 * 
 */
#include <syslog.h>
#include "WouoUI.h" //包含主要的wouoUI头文件
#include "WouoUI_user.h" //包含用户例子文件
#include "ec11.h"
#include "utils.h"
#include "OLED.h"


int main(void) { 
    openlog("oled_page", LOG_PID, LOG_USER);
    syslog(LOG_INFO, "Program started");
    EC11_Init();  // EC11编码器初始化
    OLED_Init();  //初始化oled
    WouoUI_SelectDefaultUI(); //选择默认UI
    WouoUI_AttachSendBuffFun(OLED_SendBuff); //绑定刷屏函数
    TestUI_Init(); //用户UI的初始化
    bool goHome = true;
    double startWaitRefreshHomeTime = 0;
    double inforShowStartTime = 0;
    syslog(LOG_ERR, "Init done");
    while (1)
    {
        if (goHome) {
            if (get_current_time() - startWaitRefreshHomeTime > 0.1) {  // 数据采集周期100m一次
                char ip_buffer[128];
                get_raspberry_pi_ip(ip_buffer, sizeof(ip_buffer));
                
                char cpuTempBuffer[128];
                get_cpu_temperature(cpuTempBuffer, sizeof(cpuTempBuffer));

                char memBuffer[128];
                get_memory_usage(memBuffer, sizeof(memBuffer));

                char cpuUsageBuffer[128];
                get_cpu_usage(cpuUsageBuffer, sizeof(cpuUsageBuffer));

                char diskUsageBuffer[128];
                get_disk_usage(diskUsageBuffer, sizeof(diskUsageBuffer));

                char timeBuffer[128];
                getCurrentSystemTime(timeBuffer, sizeof(timeBuffer));
                startWaitRefreshHomeTime = get_current_time();

                 // 打印到终端
                // printf("Raspberry Pi IP Address: %s\n", ip_buffer);
                if (get_current_time() - inforShowStartTime > 1) { // 显示周期 每秒一次
                    OLED_Clear();
                    OLED_PrintfMix(0, 0, OLED_12X12_FULL, OLED_7X12_HALF, "IP:%s", ip_buffer);
                    OLED_PrintfMix(0, 12*1, OLED_12X12_FULL, OLED_7X12_HALF, "CPU: %sC, %s", cpuTempBuffer, cpuUsageBuffer);
                    OLED_PrintfMix(0, 12*2, OLED_12X12_FULL, OLED_7X12_HALF, "MEM: %s", memBuffer);
                    OLED_PrintfMix(0, 12*3, OLED_12X12_FULL, OLED_7X12_HALF, "DISK: %s", diskUsageBuffer);
                    OLED_PrintfMix(0, 12*4, OLED_12X12_FULL, OLED_7X12_HALF, "%s", timeBuffer);
                    // 将显存数组的内容刷新到屏幕上
                    OLED_Update();
                    inforShowStartTime = get_current_time();
                }
            } else {
                usleep(10000); // 让cpu摆烂一会儿，10ms
            }
        } else {
            WouoUI_Proc(5);
        }
        switch (Get_EC11_State())
        {
        case CLICKED:
            if (!goHome)
            {
                WOUOUI_MSG_QUE_SEND(msg_click);
            }
            Reset_EC11_State();
            goHome = false;
            break;
        
        case DOUBLE_CLICKED:
            if (!goHome)
            {
                WOUOUI_MSG_QUE_SEND(msg_return);
            }
            Reset_EC11_State();
            goHome = false;
            break;
            
        case LONG_PRESSED:
            // WOUOUI_MSG_QUE_SEND(msg_return);
            if (goHome) {
                goHome = false;
            } else {
                goHome = true;
            }
            Reset_EC11_State();
            break;

        case UP:
            if (!goHome)
            {
                WOUOUI_MSG_QUE_SEND(msg_up);
            }
            Reset_EC11_State();
            goHome = false;
            break;
            
        case DOWN:
            if (!goHome)
            {
                WOUOUI_MSG_QUE_SEND(msg_down);
            }
            Reset_EC11_State();
            goHome = false;
            break;
        default:
            break;
        }
    }
    Deinit_EC11();
    return 0;
}